package com.rogue.member.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rogue.member.domain.HsMember;
import com.rogue.member.domain.dto.MemberDTO;
import com.rogue.member.domain.vo.MemberVO;
import com.rogue.member.mapper.HsMemberMapper;
import com.rogue.member.service.HsMemberService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
* @author Yeah
* @description 针对表【hs_member(会员表)】的数据库操作Service实现
* @createDate 2022-03-16 14:43:55
*/
@Service
public class HsMemberServiceImpl extends ServiceImpl<HsMemberMapper, HsMember>
    implements HsMemberService{

    @Resource
    private HsMemberMapper hsMemberMapper;
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public PageInfo<MemberVO> listMember(MemberDTO dto) {
        PageInfo<MemberVO> pageInfo = PageHelper.startPage(dto.getPage(), dto.getCount())
            .doSelectPageInfo(() -> hsMemberMapper.selectList(new LambdaQueryWrapper<HsMember>()
                .like(StrUtil.isNotBlank(dto.getNickname()), HsMember::getNickname,
                    dto.getNickname())));
        // 调用了 CglibUtil 来进行列表类的转换
        pageInfo.setList(CglibUtil.copyList(pageInfo.getList(), MemberVO::new));
        return pageInfo;
    }

    @Override
    public Boolean updateStatus(Long id, Integer status) {
        HsMember hsMember = new HsMember();
        if (Objects.equals(1,status)){
            status = 0;
        }
        else if (Objects.equals(0,status)){
            status = 1;
        }
        hsMember.setIsLocked(status);
        hsMember.setId(id);
        return hsMemberMapper.updateById(hsMember) > 0;

    }

    @Override
    public Boolean create(HsMember memberDTO) {
        String now = DateUtil.now();
        Date date = DateUtil.parse(now);
        memberDTO.setRegisterTime(date);
        return this.baseMapper.insert(memberDTO) > 0;
    }

    @Override
    public String getCaptcha(String phone) {
        final String code = RandomUtil.randomNumbers(6);
        redisTemplate.opsForValue().set(phone,code);
        return code;
    }
}




